var app = getApp()
Page({
  data: {
    page: -1,
    url: {
      getMyYIndent: app.url.baseUrl + "/my/nIndent",
      getPay: app.url.baseUrl +"/my/goPay",
      payS: app.url.baseUrl + "/indent/paySuccess",
    },
    indentList: [],
    isNextPageData: true
  },
  onLoad: function (options) {
    this.getNextIndent()
  },
  //得到下一页数据
  getNextIndent: function () {
    if (!this.data.isNextPageData) {
      wx.showToast({
        title: app.tips.noPageData,
      })
      return
    }
    var that = this
    var data = { pageNumber: this.data.page + 1 }
    app.ajax(this.data.url.getMyYIndent, data, function (res) {
      var data = that.data.indentList
      var tempd = res.data.data
      for (var i = 0; i < tempd.length; i++)
        data.push(tempd[i])
      that.setData({ indentList: data, page: that.data.page += 1 })
      if (tempd.length <= 20) {
        that.setData({ isNextPageData: false })
      }
    })
  },
  onReachBottom: function () {
    this.getNextIndent()
  }, 
  shop_item_Tap: function (e) {
    var indentNo = e.currentTarget.dataset.indentno;
    wx.navigateTo({
      url: '../orderinfo/orderinfo?indentNo=' + indentNo
    })
  },
  pay:function(e){
    var that = this;
    var indentNo = e.currentTarget.dataset.indentno
    app.ajax(this.data.url.getPay, { id: indentNo},function(res){
      var data = res.data
      if(data.code == 2004){
        wx.navigateTo({
          url: '../paysuccess/paysuccss?iss=1',
        })
      }else if(data.code != 2000){
        wx.showToast({
          title: app.tips.error,
        })
        return
      }
      var d = data.data
      wx.requestPayment({
        timeStamp: d.timeStamp,
        nonceStr: d.nonceStr,
        package: "prepay_id="+d.packages,
        signType: d.signType,
        paySign: d.paySign,
        success: function (res) {
          console.log("支付成功")
          wx.redirectTo({
            url: '../paysuccess/paysuccess?iss=1',
          })
        }, fail: function (res) {
          console.log("支付失败")
          wx.redirectTo({
            url: '../paysuccess/paysuccess?iss=0',
          })
        }
      })
    })
  }
})

